from flask_restful import reqparse,Resource,marshal,Api
from models.courses import Comments
from models.user import User
from common.models_fields import *
from flask import Blueprint,jsonify
from models import db
import json
comments_bp = Blueprint('comment_bp',__name__,url_prefix='/comments')

api = Api(comments_bp)

class CommentsAll(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('course_id')
        args = parser.parse_args()
        course_id = args['course_id']  # 文章id
        # 根据文章id 获取 一级 评论  被评论id为 空
        comments_master = Comments.query.filter_by(course=course_id, parent_id=None).all()
        print("所有主评论>>>>>>>>>>>>>>>", comments_master)
        # 主评论为空时  直接返回  暂无评论
        if not comments_master:
            return jsonify(code=200, message="暂无评论", data=[])
        master_list = []
        for i in comments_master:
            # 获取到每一条主评论的用户信息
            user_info = marshal(i.comment_user, user_fields)
            # 将每一条主评论 序列化
            comment_data = marshal(i, comments_fields)
            # 将用户信息存入
            comment_data['user_info'] = user_info
            # 根据每条主评论的  id   找  <<子评论>>   也就是  被评论id = 主评论id时
            dev_comment = Comments.query.filter_by(course=course_id, parent_id=i.id).all()
            dev_list = []
            if dev_comment:
                # 子评论  不为空时
                for j in dev_comment:
                    # 获取到每一条子评论的用户信息
                    # user_ = marshal(UserModel.query.filter_by(id=j.user.id), user_fields)
                    user_ = marshal(j.comment_user, user_fields)
                    # 将每一条子评论 序列化
                    comment_d = marshal(j, comments_fields)
                    # 存入用户信息
                    comment_d['user_info'] = user_
                    dev_list.append(comment_d)

            # 将子评论 存入 各自的  主评论下
            comment_data['childlist'] = dev_list
            # 然后存入列表
            master_list.append(comment_data)
        # 最后返回这个列表
        print("评论列表>>>>>>>>>>>>>>>", master_list)
        return jsonify(code=200, message="获取成功", data=master_list)
api.add_resource(CommentsAll,'/all')


from common.utils.login_ouath import login_required
from common.jwt_token import check_token
class Comment(Resource):
    @login_required
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('content')
        parser.add_argument('token')
        parser.add_argument('ques_id')
        parser.add_argument('reply')
        args = parser.parse_args()
        content = args.get('content')
        token = args.get('token')
        ques_id = args.get('ques_id')
        reply = args.get('reply')

        payload = check_token(token)
        user = payload.get('uid')
        print("user",user,type(user),reply)
        print("ques_id",ques_id,type(ques_id))

        if not reply:
            comment = Comments(user=user, comment_content=content, parent_id=None, course=int(ques_id))
            db.session.add(comment)
            db.session.commit()
            return jsonify(code=200, message="评论成功")
        else:
            comment = Comments(user=user, comment_content=content, parent_id=reply, course=int(ques_id))
            db.session.add(comment)
            db.session.commit()
            return jsonify(code=200, message="评论成功")

api.add_resource(Comment,'/add_comment')

#评论点赞
import redis
class CommentZan(Resource):
    @login_required
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('com_id')
        parser.add_argument('receive_id')
        args = parser.parse_args()
        com_id = args.get('com_id')
        receive_id = args.get('receive_id')
        print(",,,,",com_id,receive_id)
        comment = Comments.query.filter_by(id=com_id).first()
        if not comment:
            return jsonify(message='此评论不存在',code=500)
        rds = redis.Redis()
        key = com_id
        zan = rds.get(key)
        print('zan',zan)
        if zan:
            Comments.query.filter_by(id=com_id).update({"zan_num":comment.zan_num - 1})
            db.session.commit()
            rds.delete(key)
        else:
            Comments.query.filter_by(id=com_id).update({"zan_num": comment.zan_num + 1})
            db.session.commit()
            rds.set(com_id, receive_id)
        return jsonify(code=200)
api.add_resource(CommentZan,'/favorite_comment')


# 收藏
from models.courses import *
class Collect(Resource):
    @login_required
    def post(self,id):

        course = Course.query.filter_by(id=id).first()
        if course.is_collection == 0:
            Course.query.filter_by(id=id).update({'is_collection':1})
            Course.query.filter_by(id=id).update({'attention':course.attention + 1})
            db.session.commit()

            return jsonify(message='收藏成功',code=200)
        if course.is_collection == 1:
            Course.query.filter_by(id=id).update({'is_collection': 0})
            Course.query.filter_by(id=id).update({'attention':course.attention - 1})
            db.session.commit()
            return jsonify(message='取消收藏成功', code=200)

api.add_resource(Collect, '/collect/<int:id>')


class CourseLearnStatus(Resource):
    def get(self):
        parser = reqparse.RequestParser()
        parser.add_argument('cid')
        args = parser.parse_args()
        cid = args['cid']  # 小节id
        # 获取所有的章
        couser = Course.query.filter_by(id=cid).first()
        chapter = couser.chapter
        print("该课程下的章>>>>>", chapter)
        data = marshal(chapter, chapters_fields)
        return jsonify(data=data, message="获取成功", code=200)






api.add_resource(CourseLearnStatus,'/learninfo')

